/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Enum.SearchGeneralEnum;
import dao.ChiTietPhieuNhapHangDAO;
import dao.PhieuNhapHangDAO;
import dao.SanPhamCuaHangDAO;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.apache.log4j.Logger;
import pojo.ChiTietPhieuNhapHang;
import pojo.PhieuNhapHang;
import pojo.SanPhamCuaHang;

/**
 *
 * @author 0
 */
public class PhieuNhapHangGUI extends javax.swing.JDialog {

    /**
     * Creates new form PhieuNhapHangGUI
     */
    private PhieuNhapHang phieuNhapHang;
    private ArrayList<ChiTietPhieuNhapHang> chiTietPhieuNhapHangs;
    private JErrorProvider errorProvider;
    private SanPhamCuaHang selectedSanPhamCuaHang;
    private static Logger logger = Logger.getLogger(PhieuNhapHangGUI.class);
    private MyEventListener ButtonLuuClickedListener;
    
    public PhieuNhapHangGUI(java.awt.Frame parent, boolean modal,PhieuNhapHang phieuNhapHang) throws Exception {
        super(parent, modal);
        initComponents();
        errorProvider=new JErrorProvider();
        this.phieuNhapHang=phieuNhapHang;
        if(phieuNhapHang.getId()==0){
            this.setTitle("Lập phiếu nhập hàng");
        }
        else{
            this.setTitle("Chi tiết phiếu nhập hàng");
            dtNgayLapPhieu.setEditable(false);
            btnSearchMaSanPhamCuaHang.setEnabled(false);
            btnThem.setEnabled(false);
            btnXoa.setEnabled(false);
            btnLuu.setEnabled(false);
        }
        SetTabOrder();
        FormatControl();
        AutoLoad();
    }
    
    private void SetTabOrder(){
        IndexedFocusTraversalPolicy indexedFocusTraversalPolicy=new IndexedFocusTraversalPolicy();
        indexedFocusTraversalPolicy.addIndexedComponents(new Component[]{dtNgayLapPhieu,txtTongTien,txtMaSanPhamCuaHang,btnSearchMaSanPhamCuaHang,txtTenSanPham,txtSoLuong,txtDonGiaNhap,txtTongTienNhap,btnThem,btnXoa,btnClear,btnLuu,btnThoat});
        setFocusTraversalPolicy(indexedFocusTraversalPolicy);
    }
    
    private void FormatControl(){
        Utility.FormatDecimalTextfield(txtTongTien);
        Utility.FormatNumberStringTextfield(txtMaSanPhamCuaHang);
        Utility.FormatNumberStringTextfield(txtSoLuong);
        Utility.FormatDecimalTextfield(txtDonGiaNhap);
        Utility.FormatDecimalTextfield(txtTongTienNhap);
        
        txtSoLuong.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                CalculateTongTienNhap();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                CalculateTongTienNhap();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                CalculateTongTienNhap();
            }
        });
        txtDonGiaNhap.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                CalculateTongTienNhap();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                CalculateTongTienNhap();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                CalculateTongTienNhap();
            }
        });
    }
    
    private void AutoLoad() throws Exception{
        if(phieuNhapHang.getNgayLapPhieu()==null){
            dtNgayLapPhieu.setDate(new Date());
        }
        if(phieuNhapHang.getId()==0){
            chiTietPhieuNhapHangs=new ArrayList<>();
        }
        else{
            chiTietPhieuNhapHangs=(ArrayList<ChiTietPhieuNhapHang>) ChiTietPhieuNhapHangDAO.LayDanhSachByPhieuNhapHangID(phieuNhapHang.getId());
            dtNgayLapPhieu.setDate(phieuNhapHang.getNgayLapPhieu());
            txtTongTien.setValue(phieuNhapHang.getTongTienNhap());
        }
        Utility.CreateColumnsFromDB(ChiTietPhieuNhapHang.class, chiTietPhieuNhapHangs, tbChiTietPhieuNhapHang, new String[]{"Mã sản phẩm cửa hàng","Tên sản phẩm","Số lượng","Đơn giá nhập","Tổng tiền nhập"}, new String[]{"id"});
    }
    
    private int CheckDataBeforeThem(){
        int dem=0;
        errorProvider.clear();
        if(txtMaSanPhamCuaHang.getText().isEmpty()){
            errorProvider.setError(txtMaSanPhamCuaHang, "required");
            dem++;
        }
        if(txtSoLuong.getText().isEmpty()){
            errorProvider.setError(txtSoLuong, "required");
            dem++;
        }
        if(txtDonGiaNhap.getText().isEmpty()){
            errorProvider.setError(txtDonGiaNhap, "required");
            dem++;
        }
        return dem;
    }
    
    private void CalculateTongTienNhap(){        
        if(!txtSoLuong.getText().isEmpty() && !txtDonGiaNhap.getText().isEmpty()){
            txtTongTienNhap.setValue(Integer.parseInt(txtSoLuong.getText())*Double.parseDouble(txtDonGiaNhap.getText().replace(",", "")));
        }
        else{
            txtTongTienNhap.setValue(null);
        }
    }
    
    private void CalculateTongTien(){
        Double d=0.0;
        for(ChiTietPhieuNhapHang chiTietPhieuNhapHang:chiTietPhieuNhapHangs){
            d+=chiTietPhieuNhapHang.getTongTienNhap();
        }
        if(d==0){
            txtTongTien.setValue(null);
        }
        else{
            txtTongTien.setValue(d);
        }
    }
    
    private int CheckDataBeforeLuu(){
        int dem=0;
        errorProvider.clear();
        if(dtNgayLapPhieu.getDate()==null){
            errorProvider.setError(dtNgayLapPhieu, "required");
            dem++;
        }
        if(dem==0 && chiTietPhieuNhapHangs.isEmpty()){
            JOptionPane.showMessageDialog(this, "Không có chi tiết phiếu nhập hàng nên không thể tạo phiếu nhập hàng");
            dem++;
        }
        return dem;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        dtNgayLapPhieu = new org.jdesktop.swingx.JXDatePicker();
        jLabel2 = new javax.swing.JLabel();
        txtTongTien = new javax.swing.JFormattedTextField();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtMaSanPhamCuaHang = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        btnSearchMaSanPhamCuaHang = new javax.swing.JButton();
        txtTenSanPham = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtSoLuong = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtTongTienNhap = new javax.swing.JFormattedTextField();
        btnThem = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbChiTietPhieuNhapHang = new org.jdesktop.swingx.JXTable();
        jLabel7 = new javax.swing.JLabel();
        txtDonGiaNhap = new javax.swing.JFormattedTextField();
        btnLuu = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Phiếu nhập hàng");

        jLabel1.setText("Ngày lập phiếu");

        jLabel2.setText("Tổng tiền");

        txtTongTien.setEditable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Chi tiết phiếu nhập hàng"));

        jLabel3.setText("Mã sản phẩm cửa hàng");

        txtMaSanPhamCuaHang.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtMaSanPhamCuaHangFocusLost(evt);
            }
        });

        jLabel4.setText("Tên sản phẩm");

        btnSearchMaSanPhamCuaHang.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/SearchIconImage.gif"))); // NOI18N
        btnSearchMaSanPhamCuaHang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchMaSanPhamCuaHangActionPerformed(evt);
            }
        });

        txtTenSanPham.setEditable(false);

        jLabel5.setText("Số lượng");

        jLabel6.setText("Tổng tiền nhập");

        txtTongTienNhap.setEditable(false);

        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnXoa.setText("Xoá");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnClear.setText("Clear");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        tbChiTietPhieuNhapHang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tbChiTietPhieuNhapHang);

        jLabel7.setText("Đơn giá nhập");

        txtDonGiaNhap.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtDonGiaNhapFocusLost(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap(229, Short.MAX_VALUE)
                        .addComponent(btnThem)
                        .addGap(18, 18, 18))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtMaSanPhamCuaHang)
                            .addComponent(txtTenSanPham, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                            .addComponent(txtTongTienNhap))
                        .addGap(24, 24, 24)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnSearchMaSanPhamCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel5))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtDonGiaNhap, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                            .addComponent(txtSoLuong))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnXoa)
                        .addGap(18, 18, 18)
                        .addComponent(btnClear)
                        .addGap(199, 199, 199))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(txtSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(txtMaSanPhamCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnSearchMaSanPhamCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtTenSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(txtDonGiaNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtTongTienNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnXoa)
                    .addComponent(btnClear))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE))
        );

        btnLuu.setText("Lưu");
        btnLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLuuActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(dtNgayLapPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(81, 81, 81)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(txtTongTien, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnLuu)
                .addGap(18, 18, 18)
                .addComponent(btnThoat)
                .addGap(245, 245, 245))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(dtNgayLapPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtTongTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLuu)
                    .addComponent(btnThoat))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSearchMaSanPhamCuaHangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchMaSanPhamCuaHangActionPerformed
        try {
            // TODO add your handling code here:
            DialogGeneralSeach dialogGeneralSeach = new DialogGeneralSeach(null, true, SearchGeneralEnum.SanPhamCuaHang);
            final int x = (getWidth() - dialogGeneralSeach.getWidth()) + getX() / 2;
            final int y = (getHeight() - dialogGeneralSeach.getHeight());
            dialogGeneralSeach.setLocation(x, y);
            dialogGeneralSeach.setButtonOKClickedListenner(new MyEventListener() {
                @Override
                public void myEventOccured(Object evt) {
                    setSelectedSanPhamCuaHang((SanPhamCuaHang) evt);
                }
            });
            dialogGeneralSeach.setVisible(true);
        } catch (Exception ex) {
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnSearchMaSanPhamCuaHangActionPerformed

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
        // TODO add your handling code here:
        Utility.ClearControlInPanel(jPanel1);
        setSelectedSanPhamCuaHang(null);
    }//GEN-LAST:event_btnClearActionPerformed

    private void txtMaSanPhamCuaHangFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtMaSanPhamCuaHangFocusLost
        // TODO add your handling code here:
        try {
            if (!txtMaSanPhamCuaHang.getText().isEmpty()) {
                if (selectedSanPhamCuaHang == null || Integer.parseInt(txtMaSanPhamCuaHang.getText()) != selectedSanPhamCuaHang.getId()) {
                    SanPhamCuaHang sanPhamCuaHang = (SanPhamCuaHang) SanPhamCuaHangDAO.LayTheoID(SanPhamCuaHang.class, Integer.parseInt(txtMaSanPhamCuaHang.getText()));
                    if (sanPhamCuaHang == null) {
                        JOptionPane.showMessageDialog(this, "Không tìm thấy mã sản phẩm cửa hàng này trong hệ thống");
                    }
                    setSelectedSanPhamCuaHang(sanPhamCuaHang);
                }
            } else {
                setSelectedSanPhamCuaHang(null);
            }
        } catch (Exception ex) {
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_txtMaSanPhamCuaHangFocusLost

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        // TODO add your handling code here:
        if(CheckDataBeforeThem()>0){
            return;
        }
        ChiTietPhieuNhapHang chiTietPhieuNhapHang=null;
        for(ChiTietPhieuNhapHang ctpnh:chiTietPhieuNhapHangs){
            if(ctpnh.getSanPhamCuaHang().getId()==selectedSanPhamCuaHang.getId()){
                chiTietPhieuNhapHang=ctpnh;
                break;
            }
        }
        if(chiTietPhieuNhapHang==null){
            chiTietPhieuNhapHang=new ChiTietPhieuNhapHang(0, selectedSanPhamCuaHang, phieuNhapHang, Integer.parseInt(txtSoLuong.getText()), Double.parseDouble(txtDonGiaNhap.getValue().toString()), Double.parseDouble(txtTongTienNhap.getValue().toString()), false);
            chiTietPhieuNhapHangs.add(chiTietPhieuNhapHang);
        }
        else{
            chiTietPhieuNhapHang.setSoLuong(chiTietPhieuNhapHang.getSoLuong()+Integer.parseInt(txtSoLuong.getText()));
            chiTietPhieuNhapHang.setTongTienNhap(chiTietPhieuNhapHang.getTongTienNhap()+(Double)txtTongTienNhap.getValue());
        }
        Utility.CreateColumnsFromDB(ChiTietPhieuNhapHang.class, chiTietPhieuNhapHangs, tbChiTietPhieuNhapHang, new String[]{"Mã sản phẩm cửa hàng","Tên sản phẩm","Số lượng","Đơn giá nhập","Tổng tiền nhập"}, new String[]{"id"});
        CalculateTongTien();
    }//GEN-LAST:event_btnThemActionPerformed

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        // TODO add your handling code here:
        List<ChiTietPhieuNhapHang> ctpnhs=(List<ChiTietPhieuNhapHang>) Utility.GetSelectedRows(tbChiTietPhieuNhapHang, chiTietPhieuNhapHangs);
        if(!ctpnhs.isEmpty()){
            if (JOptionPane.showConfirmDialog(this, "Bạn có chắc muốn xoá không?", "Question", JOptionPane.YES_NO_OPTION) == 0) {
                Utility.RemoveSelectedRows(ChiTietPhieuNhapHang.class, tbChiTietPhieuNhapHang, chiTietPhieuNhapHangs, new String[]{"Mã sản phẩm cửa hàng","Tên sản phẩm","Số lượng","Đơn giá nhập","Tổng tiền nhập"}, new String[]{"id"});
                CalculateTongTien();
            }
        }
        else{
            JOptionPane.showMessageDialog(this, "Xin chọn dữ liệu trên lưới để xoá");
        }
    }//GEN-LAST:event_btnXoaActionPerformed

    private void btnLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLuuActionPerformed
        try {
            // TODO add your handling code here:
            if(ButtonLuuClickedListener==null){
                return;
            }
            if(CheckDataBeforeLuu()>0){
                return;
            }
            phieuNhapHang=new PhieuNhapHang(0, MainGUI.nguoiDung.getCuaHang(), dtNgayLapPhieu.getDate(), false, (Double)txtTongTien.getValue(), null);
            Integer id=PhieuNhapHangDAO.Them(phieuNhapHang, chiTietPhieuNhapHangs);
            phieuNhapHang.setId(id);
            JOptionPane.showMessageDialog(this, "Lập phiếu nhập hàng thành công");
            ButtonLuuClickedListener.myEventOccured(phieuNhapHang);
            dispose();
        } catch (Exception ex) {
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnLuuActionPerformed

    private void txtDonGiaNhapFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDonGiaNhapFocusLost
        // TODO add your handling code here:
        if(txtDonGiaNhap.getText().isEmpty()){
            txtDonGiaNhap.setValue(null);
        }
    }//GEN-LAST:event_txtDonGiaNhapFocusLost

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnLuu;
    private javax.swing.JButton btnSearchMaSanPhamCuaHang;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnXoa;
    private org.jdesktop.swingx.JXDatePicker dtNgayLapPhieu;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXTable tbChiTietPhieuNhapHang;
    private javax.swing.JFormattedTextField txtDonGiaNhap;
    private javax.swing.JTextField txtMaSanPhamCuaHang;
    private javax.swing.JTextField txtSoLuong;
    private javax.swing.JTextField txtTenSanPham;
    private javax.swing.JFormattedTextField txtTongTien;
    private javax.swing.JFormattedTextField txtTongTienNhap;
    // End of variables declaration//GEN-END:variables

    /**
     * @param sanPhamCuaHang the sanPhamCuaHang to set
     */
    public void setSelectedSanPhamCuaHang(SanPhamCuaHang selectedSanPhamCuaHang) {
        this.selectedSanPhamCuaHang = selectedSanPhamCuaHang;
        txtSoLuong.setText(null);
        txtDonGiaNhap.setText(null);
        if(selectedSanPhamCuaHang!=null){
            txtMaSanPhamCuaHang.setText(String.valueOf(selectedSanPhamCuaHang.getId()));
            txtTenSanPham.setText(selectedSanPhamCuaHang.getSanPham().getTenSanPham());
        }
        else{
            txtMaSanPhamCuaHang.setText(null);
            txtTenSanPham.setText(null);
        }
    }

    /**
     * @param ButtonLuuClickedListener the ButtonLuuClickedListener to set
     */
    public void setButtonLuuClickedListener(MyEventListener ButtonLuuClickedListener) {
        this.ButtonLuuClickedListener = ButtonLuuClickedListener;
    }
}
